﻿@using CubicFWNet.Data.Models
@using CubicFWNet.Data.ViewModal
@using Newtonsoft.Json
@model Arap
@{
    var order = ViewBag.Order as Order;
    var customer = ViewBag.Customer as Customer;
    var financeInvoice = ViewBag.FinanceInvoice as FinanceInvoice;
    var amount = ViewBag.Amount;
    var operationName = ViewBag.OperationName;
    var operationTime = ViewBag.OperationTime;
    var cha = amount - Model.ActualMoney;
}
<section class="content-header">
    <h1>
        应收账款查询
        @*@if (Model != null)
            {
                if (Model.Status == -1)
                {
                    @Html.Raw("开票查询")
                }
                if (Model.Status == 1)
                {
                    @Html.Raw("售后查询")
                }
            }
            &nbsp;*@
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("YingshouQuery", "FinanceCost")">应收账款查询</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (Model != null && order != null && customer != null && financeInvoice != null)
                {
                    <form class="form-horizontal" id="frmedit" action="@Url.Action("UpdateArap", "FinanceCost")" method="post">
                        <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                        <input type="hidden" id="Type" name="Type" value="@Model.Type" />
                        <div class="box-body">
                            <div id="json1-content" class="col-md-12">
                                <table class="footable table table-stripped table-bordered table-hover" id="footable">
                                    <thead>
                                        <tr>
                                            <th data-sort-ignore="true">
                                                发票号
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                订单号
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                客户名称
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                应收金额
                                            </th>
                                            <th data-sort-ignore="true" class="col-md-2">
                                                实际收金额
                                            </th>
                                            <th data-sort-ignore="true" class="col-md-2">
                                                未收
                                            </th>
                                            @if (Model.Status == 1)
                                            {
                                                <th data-sort-ignore="true" class="col-md-2">
                                                    操作时间
                                                </th>
                                            }
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                查看开票
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody id="data1-content">
                                        <tr>
                                            <td>
                                                @if (Model.InvoiceType == 1)
                                                {
                                                    <input type="text" class="form-control" id="FinanceNumber" name="FinanceNumber" value="@Model.FinanceNumber">
                                                }
                                                else
                                                {
                                                    @Model.FinanceNumber
                                                }
                                            </td>
                                            <td>@order.Number</td>
                                            <td>@customer.Name</td>
                                            <td>
                                                @amount
                                                <input type="hidden" class="form-control okeydown" id="ShouldMoney" name="ShouldMoney" value="@amount" readonly="readonly">
                                            </td>
                                            <td>
                                                <input type="hidden" class="form-control okeydown" id="ActualMoney" name="ActualMoney" value="@Model.ActualMoney" readonly="readonly">
                                                @Model.ActualMoney
                                            </td>
                                            <td>@cha</td>
                                            @if (Model.Status == 1)
                                            {
                                                <td>@Model.CreateTime</td>
                                            }
                                            <td>
                                                <span class="btn bg-blue btn-xs" onclick="duizhangQueryInfo('@Model.FinanceInvoiceId')">查看</span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            @if (Model.InvoiceType == 1)
                            {
                                <a role="button" style="margin-left: 20px;" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmitInfo()">修改票号</a>
                            }
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div id="json-content" class="col-md-12 ">
                                @*<input type="hidden" id="modalData" value="@financeInvoice.ProductData" readonly="readonly">*@
                                <table class="footable table table-stripped table-bordered table-hover" id="footable1">
                                    <thead>
                                        <tr>
                                            <th data-sort-ignore="true">
                                                品名
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                尺寸宽-mm
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                尺寸长-mm
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                基本单位
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                数量
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                价格
                                            </th>
                                            <th data-hide="phone,table" data-sort-ignore="true">
                                                增值税
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                金额
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                累计金额
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true" class="col-md-2">
                                                本次金额
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true" class="col-md-2">
                                                时间
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody id="data-content">
                                        @if (financeInvoice.ProductData != null)
                                        {
                                            var products = JsonConvert.DeserializeObject<List<dynamic>>(financeInvoice.ProductData);
                                            foreach (var item in products)
                                            {
                                                var cumulativemoney = 0.00;
                                                if (item.CumulativeMoney != null)
                                                {
                                                    cumulativemoney = item.CumulativeMoney;
                                                }
                                                <tr class="tr">
                                                    <td>
                                                        @item.ProductName
                                                        <input type="hidden" class="form-control" data-des="产品Id" id="ProductId" name="ProductId" value="@item.ProductId">
                                                        <input type="hidden" class="form-control" data-des="品名" id="ProductName" name="ProductName" value="@item.ProductName">
                                                    </td>
                                                    <td>
                                                        @item.ProductSizeWidth
                                                        <input type="hidden" class="form-control" data-des="尺寸宽-mm" id="ProductSizeWidth" name="ProductSizeWidth" value="@item.ProductSizeWidth">
                                                    </td>
                                                    <td>
                                                        @item.ProductSizeLenth
                                                        <input type="hidden" class="form-control" data-des="尺寸长-mm" id="ProductSizeLenth" name="ProductSizeLenth" value="@item.ProductSizeLenth">
                                                    </td>
                                                    <td>
                                                        @item.ProductUnit
                                                        <input type="hidden" class="form-control" data-des="基本单位" id="ProductUnit" name="ProductUnit" value="@item.ProductUnit">
                                                    </td>
                                                    <td>
                                                        <input type="hidden" class="form-control itemAmount" data-des="数量" id="ProductAmount" name="ProductAmount" value="@item.ProductAmount" readonly="readonly">
                                                        @item.ProductAmount
                                                    </td>
                                                    <td>
                                                        <input type="hidden" class="form-control itemPrice" data-des="价格" id="ProductPrice" name="ProductPrice" value="@item.ProductPrice" readonly="readonly">
                                                        @item.ProductPrice
                                                    </td>
                                                    <td>
                                                        @item.ProductTax
                                                        <input type="hidden" class="form-control" data-des="增值税" id="ProductTax" name="ProductTax" value="@item.ProductTax">
                                                    </td>
                                                    <td>
                                                        @item.TotalMoney
                                                        <input type="hidden" class="form-control itemMoney" data-des="金额" id="TotalMoney" name="TotalMoney" value="@item.TotalMoney.ToString("f2")" readonly="readonly">
                                                    </td>
                                                    <td>
                                                        @cumulativemoney
                                                        <input type="hidden" class="form-control" data-des="累计金额" id="CumulativeMoney" name="CumulativeMoney" value="@cumulativemoney.ToString("f2")" readonly="readonly">
                                                    </td>
                                                    <td>
                                                        @if (item.TotalMoney <= cumulativemoney)
                                                        {
                                                            <input type="hidden" class="form-control okeydown itemActualMoney" data-des="本次金额" id="ItemActualMoney" name="ItemActualMoney" data-totalmoney="@item.TotalMoney" data-cumulativemoney="@cumulativemoney">
                                                        }
                                                        else
                                                        {
                                                            <input type="text" class="form-control okeydown itemActualMoney" data-des="本次金额" id="ItemActualMoney" name="ItemActualMoney" data-totalmoney="@item.TotalMoney" data-cumulativemoney="@cumulativemoney">
                                                        }
                                                    </td>
                                                    <td>
                                                        @if (item.TotalMoney <= cumulativemoney)
                                                        {
                                                            <input type="text" class="form-control Timedatepicker itemStrEndTime" id="StrEndTime" name="StrEndTime" value="@item.StrEndTime" readonly="readonly" />
                                                        }
                                                        else
                                                        {
                                                            <input type="text" class="form-control Timedatepicker itemStrEndTime" id="StrEndTime" name="StrEndTime" value="@item.StrEndTime" />
                                                        }
                                                        <input type="hidden" class="form-control" id="StrCreateTime" name="StrCreateTime" value="@item.StrCreateTime" />
                                                    </td>
                                                </tr>
                                            }
                                        }
                                    </tbody>
                                </table>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">备注</label>
                                <div class="col-sm-9">
                                    @if (Model.Status == -1)
                                    {
                                        <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo">@Model.Memo</textarea>
                                    }
                                    else
                                    {
                                        <textarea class="form-control" rows="3" placeholder="" name="Memo" id="Memo" readonly="readonly">@Model.Memo</textarea>
                                    }
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>

                            <div class="form-group col-md-6">
                                <label for="operationName" class="col-sm-3 control-label">操作人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationName" name="operationName" value="@operationName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="operationTime" class="col-sm-3 control-label">日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="box-footer" style="width: 80%; margin-left: 20px;">
                            @if (Model.Status == -1)
                            {
                                <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(1)">确认</a>
                            }

                            <span id="historyBack" class="btn btn-primary submitbtn">返回</span>
                            @*<a class="btn btn-primary submitbtn" href="@Url.Action("ReceivableQuery", "FinanceCost")">返回</a>*@
                        </div>
                    </form>
                }
            </div>
        </div>
    </div>
</section>

<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

@section Scripts{
    <script type="text/javascript">
        //日期控件
        $(".Timedatepicker").datepicker({
            autoclose: true,
            language: 'zh', //汉化
            format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
            minView: "month"//选择日期后，不会再跳转去选择时分秒
        });

        $(document).ready(function () {
            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.chousetype = type,
                    postData.Data = getParam();
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            //$.reload();
                            $.modalAlert("操作成功", "success");
                            window.location.href = '@Url.Action("YingshouQuery", "FinanceCost")';
                            @*if (type == 1) {
                                window.location.href = '@Url.Action("ReceivableQuery", "FinanceCost")';
                            }
                            if (type == 2) {
                                window.location.href = '@Url.Action("SendTicketQuery", "CustomerService")';
                            }*@
                        }
                    });
                }
            });

            /*var yy = document.getElementById("modalData").value;
            if (!yy || yy != "") {
                var data = JSON.parse(document.getElementById("modalData").value);
                var $content = $('#data-content');
                var html = "";
                var totalmoney = 0;
                for (var i = 0; i < data.length; i++) {
                    var cumulativeMoney = 0;
                    if (data[i].CumulativeMoney) {
                        cumulativeMoney = data[i].CumulativeMoney;
                    }
                    html += "<tr class='tr'>";
                    html += "<td>" + data[i].ProductName + "</td>";
                    html += "<td>" + data[i].ProductSizeWidth + "</td>";
                    html += "<td>" + data[i].ProductSizeWidth + "</td>";
                    html += "<td>" + data[i].ProductUnit + "</td>";
                    html += "<td>" + data[i].ProductAmount + "</td>";
                    html += "<td>" + data[i].ProductPrice + "</td>";
                    html += "<td>" + data[i].ProductTax + "</td>";
                    html += "<td>" + data[i].TotalMoney + "</td>";
                    html += "<td>" + cumulativeMoney + "</td>";
                    //html += "<td><input type='text' class='form-control okeydown itemCumulativeMoney' id='CumulativeMoney' name='CumulativeMoney' /></td>";
                    html += "<td><input type='text' class='form-control okeydown itemActualMoney' id='itemActualMoney' name='itemActualMoney' data-totalMoney='" + data[i].TotalMoney + "' data-cumulativeMoney='" + cumulativeMoney + "'/></td>";
                    html += "<td><input type='text' class='form-control Timedatepicker itemStrEndTime' id='StrEndTime' name='StrEndTime'/>" +
                            "<input type='hidden' class='form-control' id='StrCreateTime' name='StrCreateTime' value='" + data[i].StrCreateTime + "'/>" +
                            "<input type='hidden' class='form-control' id='ProductId' name='ProductId' value='" + data[i].ProductId + "'/></td>";
                    html += "</tr>";
                }
                $content.append(html);
            }*/
            $('#footable').footable();
            $('#footable1').footable();
        });

        var type = 1;

        function formsubmit(t) {
            type = t;
            $("#Type").val(t);
            $("#frmedit").submit();
        }


        //修改
        function formsubmitInfo() {
            var financeNumber = $("#FinanceNumber").val();
            var arapId = $("#Id").val();
            $.ajax({
                url: "/FinanceCost/UpdateYingshouInfoNumber",
                type: "post",
                data: {
                    arapId: arapId,
                    financeNumber: financeNumber
                },
                success: function (data) {
                    if (data && JSON.parse(data)) {
                        var d = JSON.parse(data);
                        if (d.Code == 200) {
                            $.modalAlert("修改成功", "success");
                            window.location.href = '@Url.Action("YingshouInfo", "FinanceCost",new {id=Model.Id})';
                        } else {
                            $.modalAlert("修改失败", "warning");
                        }
                    } else {
                        $.modalAlert("参数错误", "warning");
                    }
                },
                error: function (xhr, err, state) {
                    //alert("e");
                }
            });
        }

        //$(".okeydown").on("keydown", function () {
        $(document).on("keydown", ".okeydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });

        $(document).on("keyup", ".itemActualMoney", function () {
            var itemMoney = $(this).val();//本次输入金额
            var cumulativeMoney = $(this).attr('data-cumulativeMoney');//累计金额
            var totalMoney = $(this).attr('data-totalMoney');//总金额
            //console.log(itemMoney + "+" + cumulativeMoney + "+" + totalMoney);
            if (isNaN(itemMoney)) {
                itemMoney = 0;
            }
            if (isNaN(cumulativeMoney)) {
                cumulativeMoney = 0;
            }
            if (isNaN(totalMoney)) {
                totalMoney = 0;
            }
            var sub = (((totalMoney / 1) - (cumulativeMoney / 1)) - (itemMoney / 1));
            //$(this).val((itemMoney / 1).toFixed(2));
            if ((sub / 1) < 0) {
                $(this).val(((totalMoney / 1) - (cumulativeMoney / 1)).toFixed(2));
            }

        });

        function getParam() {
            var postdata = [];
            //主键
            $("#json-content").find('.tr').each(function (r, i) { //input,select,textarea
                var postdataItem = {};
                $(i).find('input').each(function (m) {
                    var $this = $(this);
                    var id = $this.attr('id');
                    var value = $this.val();
                    if (id == "ItemActualMoney" && (isNaN(value) || value < 0 || value == "")) {
                        value = 0;
                    }
                    postdataItem[id] = value;
                });
                postdata.push(postdataItem);
            });
            return JSON.stringify(postdata);
        }


        $("#historyBack").click(function () {
            window.history.go(-1);  //返回上一页
            //window.history.back();  //返回上一页
        });

        function duizhangQueryInfo(fid) {
            var options = {
                id: null,
                title: '开票信息',
                width: "100%",
                height: "100%",
                url: "/FinanceCost/FinanceInvoiceLayout?fid=" + fid,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }
    </script>
}